<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    <title>django orm | Cucy的博客</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="django orm">
    <meta name="description" content="模型from django.db import modelsclass UserType(models.Model):    # 用户类型    caption = models.CharField(max_length=16)class UserInfo(models.Model):    # 用户信息    username = models.CharField(max_length=32)">
<meta property="og:type" content="article">
<meta property="og:title" content="django orm">
<meta property="og:url" content="http://zhourudong.cn/2017/05/31/38-django-orm/index.html">
<meta property="og:site_name" content="Cucy的博客">
<meta property="og:description" content="模型from django.db import modelsclass UserType(models.Model):    # 用户类型    caption = models.CharField(max_length=16)class UserInfo(models.Model):    # 用户信息    username = models.CharField(max_length=32)">
<meta property="og:updated_time" content="2019-02-22T05:28:07.823Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="django orm">
<meta name="twitter:description" content="模型from django.db import modelsclass UserType(models.Model):    # 用户类型    caption = models.CharField(max_length=16)class UserInfo(models.Model):    # 用户信息    username = models.CharField(max_length=32)">
    
        <link rel="alternate" type="application/atom+xml" title="Cucy的博客" href="/atom.xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.6.13">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.png">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">cucy</h5>
          <a href="mailto:292016176@qq.com" title="292016176@qq.com" class="mail">292016176@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/cucy" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">django orm</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">django orm</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-05-31T09:32:22.000Z" itemprop="datePublished" class="page-time">
  2017-05-31
</time>


            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#模型"><span class="post-toc-number">1.</span> <span class="post-toc-text">模型</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#数据"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">数据</span></a></li></ol></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#查询"><span class="post-toc-number">2.</span> <span class="post-toc-text">查询</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#一对多"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">一对多</span></a></li></ol></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#多对多"><span class="post-toc-number">3.</span> <span class="post-toc-text">多对多</span></a></li></ol>
        </nav>
    </aside>
    
<article id="post-38-django-orm"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">django orm</h1>
        <div class="post-meta">
            <time class="post-time" title="2017-05-31 17:32:22" datetime="2017-05-31T09:32:22.000Z"  itemprop="datePublished">2017-05-31</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h1 id="模型"><a href="#模型" class="headerlink" title="模型"></a>模型</h1><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">from django.db import models</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">class UserType(models.Model):</span><br><span class="line">    # 用户类型</span><br><span class="line">    caption = models.CharField(max_length=16)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">class UserInfo(models.Model):</span><br><span class="line">    # 用户信息</span><br><span class="line">    username = models.CharField(max_length=32)</span><br><span class="line">    pwd = models.CharField(max_length=32)</span><br><span class="line">    user_type = models.ForeignKey(&apos;UserType&apos;)</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<h2 id="数据"><a href="#数据" class="headerlink" title="数据"></a>数据</h2><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">sqlite&gt; SELECT * FROM app01_userinfo;</span><br><span class="line">1|张珊|12344|1</span><br><span class="line">2|李四|123456|3</span><br></pre></td></tr></table></figure>
<h1 id="查询"><a href="#查询" class="headerlink" title="查询"></a>查询</h1><ul>
<li><p>基本查询</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">In [10]: UserInfo.objects.all()</span><br><span class="line">Out[10]: &lt;QuerySet [&lt;UserInfo: UserInfo object&gt;, &lt;UserInfo: UserInfo object&gt;]&gt;</span><br></pre></td></tr></table></figure>
</li>
<li><p>values</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">In [12]: UserInfo.objects.all().values(&quot;username&quot;)</span><br><span class="line">Out[12]: &lt;QuerySet [&#123;&apos;username&apos;: &apos;张珊&apos;&#125;, &#123;&apos;username&apos;: &apos;李四&apos;&#125;]&gt;</span><br></pre></td></tr></table></figure>
</li>
<li><p>values_list</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">In [14]: UserInfo.objects.all().values_list(&quot;username&quot;, &quot;pwd&quot;)</span><br><span class="line">Out[14]: &lt;QuerySet [(&apos;张珊&apos;, &apos;12344&apos;), (&apos;李四&apos;, &apos;123456&apos;)]&gt;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="一对多"><a href="#一对多" class="headerlink" title="一对多"></a>一对多</h2><p><em>模型定义</em><br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserType</span><span class="params">(models.Model)</span>:</span></span><br><span class="line">    <span class="comment"># 用户类型 CEO 经理 主管 组长 员工 ...</span></span><br><span class="line">    caption = models.CharField(max_length=<span class="number">16</span>)</span><br><span class="line">    </span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserInfo</span><span class="params">(models.Model)</span>:</span></span><br><span class="line">    <span class="comment"># 用户信息</span></span><br><span class="line">    username = models.CharField(max_length=<span class="number">32</span>)</span><br><span class="line">    pwd = models.CharField(max_length=<span class="number">32</span>)</span><br><span class="line">    user_type = models.ForeignKey(<span class="string">'UserType'</span>)</span><br><span class="line">    <span class="comment"># user_type_id(生成表中字段名)</span></span><br></pre></td></tr></table></figure></p>
<ul>
<li>创建</li>
</ul>
<p>第1种方法<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">In [<span class="number">3</span>]: UserInfo.objects.create(username=<span class="string">'老王'</span>,pwd=<span class="string">'123'</span>,user_type=UserType.objects.get(id=<span class="number">2</span>)) </span><br><span class="line">Out[<span class="number">3</span>]: &lt;UserInfo: UserInfo object&gt;</span><br></pre></td></tr></table></figure></p>
<p><em>多一次数据库查询</em></p>
<p>第2种方法<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">In [<span class="number">4</span>]: UserInfo.objects.create(username=<span class="string">'王五'</span>,pwd=<span class="string">'123'</span>,user_type_id=<span class="number">2</span>)</span><br><span class="line">Out[<span class="number">4</span>]: &lt;UserInfo: UserInfo object&gt;</span><br></pre></td></tr></table></figure></p>
<p>第三种方法<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">data = &#123;<span class="string">'pwd'</span>: <span class="string">'123'</span>, <span class="string">'user_type_id'</span>: <span class="number">2</span>, <span class="string">'username'</span>: <span class="string">'张无忌'</span>&#125;</span><br><span class="line">In [<span class="number">13</span>]: u = UserInfo(**data)</span><br><span class="line">In [<span class="number">14</span>]: u.save()</span><br></pre></td></tr></table></figure></p>
<p>第四种方法<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">In [<span class="number">15</span>]: u = UserInfo()</span><br><span class="line"></span><br><span class="line">In [<span class="number">16</span>]: u.username = <span class="string">"周芷若"</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">17</span>]: u.pwd = <span class="string">"123"</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">18</span>]: u.user_type = <span class="number">1</span> <span class="comment"># 报错</span></span><br><span class="line">In [<span class="number">19</span>]: u.user_type_id = <span class="number">1</span></span><br><span class="line">In [<span class="number">20</span>]: u.save()</span><br></pre></td></tr></table></figure></p>
<ul>
<li>查询</li>
</ul>
<p>方法1<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">In [<span class="number">30</span>]: u_type_id = UserType.objects.get(caption=<span class="string">"CE0"</span>).id</span><br><span class="line"></span><br><span class="line">In [<span class="number">31</span>]: UserInfo.objects.filter(user_type_id=u_type_id)</span><br><span class="line">Out[<span class="number">31</span>]: &lt;QuerySet [&lt;UserInfo: UserInfo object&gt;, &lt;UserInfo: UserInfo object&gt;, &lt;UserInfo: UserInfo object&gt;]&gt;</span><br><span class="line"></span><br><span class="line">先查找出 外键id 主表过滤外键id集合</span><br></pre></td></tr></table></figure></p>
<p>方法2<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">In [<span class="number">32</span>]: UserInfo.objects.filter(user_type__caption=<span class="string">'CE0'</span>)</span><br><span class="line">Out[<span class="number">32</span>]: &lt;QuerySet [&lt;UserInfo: UserInfo object&gt;, &lt;UserInfo: UserInfo object&gt;, &lt;UserInfo: UserInfo object&gt;]&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过models模型定义的字段名(不是生成数据库的字段) user_type__caption  字段名__外键关联表的字段（两个下划线）</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#  能操作方法和单表一致</span></span><br><span class="line">In [<span class="number">38</span>]: UserInfo.objects.filter(user_type__id__gte=<span class="number">1</span>)</span><br><span class="line">Out[<span class="number">38</span>]: &lt;QuerySet [&lt;UserInfo: UserInfo object&gt;, &lt;UserInfo: UserInfo object&gt;, &lt;UserInfo: UserInfo object&gt;, &lt;UserInfo: UserInfo object&gt;, &lt;UserInfo: UserInfo object&gt;, &lt;UserInfo: UserInfo object&gt;]&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">In [<span class="number">39</span>]: UserInfo.objects.filter(user_type__caption=<span class="string">'CE0'</span>).values(<span class="string">"username"</span>, <span class="string">'pwd'</span>)</span><br><span class="line">Out[<span class="number">39</span>]: &lt;QuerySet [&#123;<span class="string">'pwd'</span>: <span class="string">'12344'</span>, <span class="string">'username'</span>: <span class="string">'张珊'</span>&#125;, &#123;<span class="string">'pwd'</span>: <span class="string">'123456'</span>, <span class="string">'username'</span>: <span class="string">'李四'</span>&#125;, &#123;<span class="string">'pwd'</span>: <span class="string">'123'</span>, <span class="string">'username'</span>: <span class="string">'周芷若'</span>&#125;]&gt;</span><br></pre></td></tr></table></figure></p>
<p>方法3<br><figure class="highlight python"><table><tr><td class="code"><pre><span class="line">只要是有关键关联 可以使用多次两下划线 __</span><br><span class="line"><span class="number">2</span>、三张表跨表操作</span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">Somthing</span><span class="params">(models.Model)</span>:</span></span><br><span class="line">        name = models.CharField(max_length=<span class="number">32</span>)</span><br><span class="line">    </span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">UserType</span><span class="params">(models.Model)</span>:</span></span><br><span class="line">        catption = models.CharField(max_length=<span class="number">32</span>)</span><br><span class="line">        s = models.ForignKey(<span class="string">'Somthing'</span>)</span><br><span class="line">        <span class="comment"># ceo，经理 ， 组长，...</span></span><br><span class="line">    </span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">UserInfo</span><span class="params">(models.Model)</span>:</span></span><br><span class="line">        </span><br><span class="line">        user = models.CharField(max_length=<span class="number">32</span>)</span><br><span class="line">        pwd = models.CharField(max_length=<span class="number">32</span>)</span><br><span class="line">        user_type = models.ForignKey(<span class="string">'UserType'</span>)</span><br><span class="line">    </span><br><span class="line">    UserInfo.objects.filter(user_type__s__name=<span class="string">'xx'</span>)</span><br></pre></td></tr></table></figure></p>
<h1 id="多对多"><a href="#多对多" class="headerlink" title="多对多"></a>多对多</h1><figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Book</span><span class="params">(models.Model)</span>:</span></span><br><span class="line">    name = models.CharField(max_length=<span class="number">64</span>, verbose_name=<span class="string">"书名"</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Tag</span><span class="params">(models.Model)</span>:</span></span><br><span class="line">    <span class="comment"># 文学 技术 地理 文科 理科 西方文学...</span></span><br><span class="line">    tag_name = models.CharField(max_length=<span class="number">32</span>, verbose_name=<span class="string">"标签"</span>)</span><br><span class="line">    b = models.ManyToManyField(<span class="string">"Book"</span>, verbose_name=<span class="string">"关联书名"</span>)</span><br><span class="line"></span><br><span class="line">会生成三张表</span><br><span class="line">    应用名_book</span><br><span class="line">    应用名_tag</span><br><span class="line">    </span><br><span class="line">    应用名_tag_b    (多对多中间表<span class="string">"b"</span>是模型中的的字段来命名) 此表保存书名的id和标签id </span><br><span class="line">    隐式创建</span><br></pre></td></tr></table></figure>
<p>数据<br><figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">sqlite&gt; SELECT * FROM app01_book;</span><br><span class="line">1|莎士比亚</span><br><span class="line">2|悲惨世界</span><br><span class="line">3|性能之巅</span><br><span class="line">4|高性能MySQL</span><br><span class="line"></span><br><span class="line">sqlite&gt; SELECT * FROM app01_tag;</span><br><span class="line">1|IT</span><br><span class="line">2|西方文学</span><br><span class="line">3|文科</span><br><span class="line">4|理科</span><br><span class="line">5|技术</span><br><span class="line"></span><br><span class="line">sqlite&gt; SELECT * FROM app01_tag_b;</span><br><span class="line">sqlite&gt;</span><br></pre></td></tr></table></figure></p>
<ul>
<li><p>增<br><em>add</em></p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">In [<span class="number">11</span>]: tag = Tag.objects.get(id=<span class="number">2</span>)</span><br><span class="line">In [<span class="number">12</span>]: tag.tag_name</span><br><span class="line">Out[<span class="number">12</span>]: <span class="string">'西方文学'</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">13</span>]: Book.objects.get(id=<span class="number">1</span>).name</span><br><span class="line">Out[<span class="number">13</span>]: <span class="string">'莎士比亚'</span></span><br><span class="line">In [<span class="number">14</span>]: Book.objects.get(id=<span class="number">2</span>).name</span><br><span class="line">Out[<span class="number">14</span>]: <span class="string">'悲惨世界'</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">In [<span class="number">15</span>]: tag.b.add(<span class="number">1</span>)  <span class="comment"># 将 tag_id=2, book_id=1插入 第三张关联表中</span></span><br><span class="line">In [<span class="number">16</span>]: tag.b.add(<span class="number">2</span>)  <span class="comment"># 将 tag_id=2, book_id=1插入 第三张关联表中</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">批量插入</span><br><span class="line">In [<span class="number">17</span>]: tag.b.add(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>) </span><br><span class="line">    <span class="comment"># 将 tag_id=2, book_id=1插入 第三张关联表中</span></span><br><span class="line">    <span class="comment"># 将 tag_id=2, book_id=2插入 第三张关联表中</span></span><br><span class="line">    <span class="comment"># 将 tag_id=2, book_id=3插入 第三张关联表中</span></span><br><span class="line">    <span class="comment"># 将 tag_id=2, book_id=4插入 第三张关联表中</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">18</span>]: tag.b.add(*[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]) <span class="comment"># 效果同上相当于参数解包</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>删</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">tag = Tag.objects.get(id=<span class="number">2</span>)</span><br><span class="line"></span><br><span class="line">In [<span class="number">19</span>]: tag.b.remove(<span class="number">1</span>)    <span class="comment"># 将 tag_id=2, book_id=1 删除此记录 </span></span><br><span class="line">In [<span class="number">20</span>]: tag.b.remove(<span class="number">2</span>,<span class="number">4</span>)  <span class="comment"># 将 tag_id=2, book_id=2 tag_id=2, book_id=4      删除此记录 </span></span><br><span class="line">In [<span class="number">21</span>]: tag.b.remove(*[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]) <span class="comment"># 原理同上</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">clear </span><br><span class="line">In [<span class="number">22</span>]: tag.b.clear()  <span class="comment"># 相当于 DELECT FROM app01_tag_b WHERE tag_id=2;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>改</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">In [<span class="number">1</span>]: <span class="keyword">from</span> app01.models <span class="keyword">import</span> *</span><br><span class="line">In [<span class="number">2</span>]: tag = Tag.objects.get(id=<span class="number">2</span>)</span><br><span class="line"></span><br><span class="line">In [<span class="number">4</span>]: tag.b.set([<span class="number">2</span>,<span class="number">3</span>]) <span class="comment">#  1、隐含操作 DELECT FROM appo1_tag_b WHER tag_id = 2</span></span><br><span class="line">                         <span class="comment">#  2、 将 tag_id=2, book_id=2 tag_id=2, book_id=3 插入</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>查</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">In [<span class="number">2</span>]: tag = Tag.objects.get(id=<span class="number">2</span>)</span><br><span class="line"></span><br><span class="line">In [<span class="number">12</span>]: tag.b.all()</span><br><span class="line">Out[<span class="number">12</span>]: &lt;QuerySet [&lt;Book: Book object&gt;, &lt;Book: Book object&gt;]&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">通过标签的外键查书名</span><br><span class="line"></span><br><span class="line">In [<span class="number">34</span>]: tag = Tag.objects.prefetch_related(<span class="string">'b'</span>).filter(id=<span class="number">2</span>).first()</span><br><span class="line"></span><br><span class="line">In [<span class="number">37</span>]: <span class="keyword">for</span> i <span class="keyword">in</span> tag.b.all():  <span class="comment"># 因为使用prefetch_related 此循环没有再次执行sql查找</span></span><br><span class="line">    ...:     <span class="keyword">print</span> (i.name)</span><br><span class="line">    ...:     </span><br><span class="line">莎士比亚</span><br><span class="line">悲惨世界</span><br><span class="line">性能之巅</span><br><span class="line">高性能mysql</span><br><span class="line"></span><br><span class="line">使用 prefetch_related 可以优化减少查询的次数</span><br><span class="line"></span><br><span class="line">mysql&gt;  SELECT `app01_tag`.`id`, `app01_tag`.`tag_name` FROM `app01_tag` WHERE `app01_tag`.`id` = <span class="number">2</span> ORDER BY `app01_tag`.`id` ASC LIMIT <span class="number">1</span>;</span><br><span class="line">+----+--------------+</span><br><span class="line">| id | tag_name     |</span><br><span class="line">+----+--------------+</span><br><span class="line">|  <span class="number">2</span> | 西方文学     |</span><br><span class="line">+----+--------------+</span><br><span class="line"><span class="number">1</span> row <span class="keyword">in</span> set (<span class="number">0.00</span> sec)</span><br><span class="line"></span><br><span class="line">mysql&gt;  SELECT `app01_tag_b`.`tag_id` AS `_prefetch_related_val_tag_id`, `app01_book`.`id`, `app01_book`.`name`</span><br><span class="line">FROM `app01_book`</span><br><span class="line">    INNER JOIN `app01_tag_b` ON `app01_book`.`id` = `app01_tag_b`.`book_id`</span><br><span class="line">WHERE `app01_tag_b`.`tag_id` IN (<span class="number">2</span>) ;</span><br><span class="line">+------------------------------+----+----------------+</span><br><span class="line">| _prefetch_related_val_tag_id | id | name           |</span><br><span class="line">+------------------------------+----+----------------+</span><br><span class="line">|                            <span class="number">2</span> |  <span class="number">1</span> | 莎士比亚       |</span><br><span class="line">|                            <span class="number">2</span> |  <span class="number">2</span> | 悲惨世界       |</span><br><span class="line">|                            <span class="number">2</span> |  <span class="number">3</span> | 性能之巅       |</span><br><span class="line">|                            <span class="number">2</span> |  <span class="number">4</span> | 高性能mysql    |</span><br><span class="line">+------------------------------+----+----------------+</span><br><span class="line"><span class="number">4</span> rows <span class="keyword">in</span> set (<span class="number">0.00</span> sec)</span><br></pre></td></tr></table></figure>
</li>
</ul>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2019-02-22T05:28:07.823Z" itemprop="dateUpdated">2019-02-22 13:28:07</time>
</span><br>


        
        <a href="/2017/05/31/38-django-orm/" target="_blank" rel="external">http://zhourudong.cn/2017/05/31/38-django-orm/</a>
        
    </div>
    <footer>
        <a href="http://zhourudong.cn">
            <img src="/img/avatar.png" alt="cucy">
            cucy
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/django-orm/">django orm</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://zhourudong.cn/2017/05/31/38-django-orm/&title=《django orm》 — Cucy的博客&pic=http://zhourudong.cn/img/avatar.png" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://zhourudong.cn/2017/05/31/38-django-orm/&title=《django orm》 — Cucy的博客&source=模型from django.db import modelsclass UserType(models.Model):    # 用户类型    capt..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://zhourudong.cn/2017/05/31/38-django-orm/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《django orm》 — Cucy的博客&url=http://zhourudong.cn/2017/05/31/38-django-orm/&via=http://zhourudong.cn" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://zhourudong.cn/2017/05/31/38-django-orm/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/08/24/44-GitLab7-9-2升级到9-4-5最新版本/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">GitLab7.9.2升级到9.4.5最新版本</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/04/20/37-python-字典/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">python 字典</h4>
      </a>
    </div>
  
</nav>



    














</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/img/wechat.jpg" data-alipay="/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循 <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>cucy &copy; 2016 - 2021</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://zhourudong.cn/2017/05/31/38-django-orm/&title=《django orm》 — Cucy的博客&pic=http://zhourudong.cn/img/avatar.png" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://zhourudong.cn/2017/05/31/38-django-orm/&title=《django orm》 — Cucy的博客&source=模型from django.db import modelsclass UserType(models.Model):    # 用户类型    capt..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://zhourudong.cn/2017/05/31/38-django-orm/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《django orm》 — Cucy的博客&url=http://zhourudong.cn/2017/05/31/38-django-orm/&via=http://zhourudong.cn" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://zhourudong.cn/2017/05/31/38-django-orm/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=http://zhourudong.cn/2017/05/31/38-django-orm/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="/js/main.min.js?v=1.6.13"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="/js/search.min.js?v=1.6.13" async></script>






<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
